package agents;

import factory.Constants;
import interfaces.ActionInterface;
import interfaces.impl.Agent;

import java.awt.Graphics;
import java.util.ArrayList;

import objects.SpetialFeature;
import objects.WorldObject;
import universe.Universe;
import actions.ActionBuildBadHouse;
import actions.ActionGetHouse;

public class AgentSearchHouse extends Agent{
	
	private ActionGetHouse actionGetHouse;
	private ActionBuildBadHouse actionBuildBadHouse;
	private double hasHouse=Constants.FALSE;
	
	private double contTime=0;
	private double timeToBuildOwnHouse=10000;//time in millis -> 10 seconds
	
	public AgentSearchHouse(){
		this.id=Constants.AGENT_SEARCH_HOUSE;
		
		listActions= new ArrayList<ActionInterface>();
		
		actionGetHouse=new ActionGetHouse(priority);
		listActions.add(actionGetHouse);
		
		actionBuildBadHouse=new ActionBuildBadHouse(priority);
		listActions.add(actionBuildBadHouse);
	}

	@Override
	public void update(WorldObject object, Universe worldI, double time) {
		
		
		if(!canUpdate(object)){
			System.out.println("Me falta alguna accion para poder actuar");
			return;
		}
		
		int cont=0;
		for(SpetialFeature f : object.getListFeatures()){
			if(f.getId() == Constants.SPETIAL_FEATURE_HOUSE){
				hasHouse = f.getValue();
				cont++;
			}
		}
		if(cont!=1){
			System.out.println("Me falta Algun ATRIBUTO para poder actuar");
			return ;
		}
		
		if(hasHouse==Constants.TRUE){
			//System.out.println("YA TENGO CASA");
			cont=0;
			return ;
		}
		//then i search a empty house
		SpetialFeature totalPersons=null;
		SpetialFeature nPersons=null;
		for(WorldObject obj : worldI.getListObjects()){
			for(SpetialFeature fe : obj.getListFeatures()){
				if(fe.getId()==Constants.SPETIAL_FEATURE_NUMBER_PERSON){
					nPersons=fe;
				}
				if(fe.getId()==Constants.SPETIAL_FEATURE_TOTAL_NUMBER_PERSON){
					totalPersons=fe;
				}
			}
			if(totalPersons!=null && nPersons!=null){
				if(nPersons.getValue()<totalPersons.getValue()){
					generateActionGetHouse(object,obj);
					return;
				}
			}
				
		}
		
		
		contTime=contTime+time;
		if(contTime<timeToBuildOwnHouse){
			return;
		}
		contTime=0;
		//here i build my own house
		System.out.println("NOOOOO TENGO CASA ->>>Voy a contruir Rancho");
		generateActionBuildBadHouse(object);
		
	}

	private void generateActionBuildBadHouse(WorldObject object) {
		
		actionBuildBadHouse.setSource(object);
		actionBuildBadHouse.setTarget(object);
		actionBuildBadHouse.setExecuteTime(1000);
		actionBuildBadHouse.setWaitTimeToExecute(2000);
		object.setNormalActionToDo(actionBuildBadHouse);
		
	}

	private void generateActionGetHouse(WorldObject source, WorldObject target) {
		actionGetHouse.setSource(source);
		actionGetHouse.setTarget(target);
		actionGetHouse.setPriority(priority);
		source.setImmediatlyActionToDo(actionGetHouse);
		
	}

	@Override
	public void paint(Graphics g, WorldObject object) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void setName(String name) {
		// TODO Auto-generated method stub
		
	}
	
	

}
